package com.qf.kafkaproducer.controller;

import com.qf.kafkapojo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author q
 * @version V1.0
 * @date 2021/12/25
 **/
@RestController
@RequestMapping("/user")
public class UserProducer {

    private final String TOPIC_NAME = "my-cluster-topic";

    static Integer mesKey = 0;
    static Integer userKey = 0;  //目前用到2了，下次从3开始

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @PostMapping("/createUser")
    //public String createUser(@RequestBody User user){
    public String createUser(@RequestBody String user){
        System.out.println("userKey -> " + userKey);
        System.out.println(user);
        kafkaTemplate.send(TOPIC_NAME, 0, "user-key-"+ userKey++, user);
        return "create user success~";
    }

    @GetMapping("/send")
    public String send(String message){
        kafkaTemplate.send(TOPIC_NAME,0,"message-key-"+ mesKey++,message);
        return "send success!";
    }
}
